package com.jiangman.weather.ui.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.tablayout.SlidingTabLayout;
import com.jiangman.weather.R;
import com.jiangman.weather.adapter.CompleteShowAdapter;
import com.jiangman.weather.adapter.MyForecastBaseAdapter;
import com.jiangman.weather.adapter.MyLivingIndexBaseAdapter;
import com.jiangman.weather.adapter.MyLocationBaseAdapter;
import com.jiangman.weather.adapter.MyPagerAdapter;
import com.jiangman.weather.base.BaseFragment;
import com.jiangman.weather.bean.WeatherAQI;
import com.jiangman.weather.bean.WeatherForecast;
import com.jiangman.weather.bean.WeatherRealtime;
import com.jiangman.weather.bean.WeatherYesterday;
import com.jiangman.weather.bean.WeatherZhiShu;
import com.jiangman.weather.db.dao.CityQueryDao;
import com.jiangman.weather.db.dao.WeatherDao;
import com.jiangman.weather.ui.activity.ChooseCityActivity;
import com.jiangman.weather.ui.activity.CityManageActivity;
import com.jiangman.weather.view.TriangleDataDisplayView;
import com.jiangman.weather.view.VerticalDataDisplayView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import util.DensityUtils;
import util.HttpUtils;
import util.SnackBarUtils;
import util.WeatherDataParser;
import util.WeatherUtils;

import static android.content.Context.MODE_PRIVATE;
import static com.jiangman.weather.R.id.iv_refresh;
import static com.jiangman.weather.R.id.iv_settings;
import static com.jiangman.weather.R.id.iv_type;
import static com.jiangman.weather.R.id.ll_alarmInfo;
import static com.jiangman.weather.R.id.ll_manage;
import static com.jiangman.weather.R.id.ll_updateTimeArea;
import static com.jiangman.weather.R.id.lv_drawerMenu;
import static com.jiangman.weather.R.id.tv_RTTemp;
import static com.jiangman.weather.R.id.tv_RTType;
import static com.jiangman.weather.R.id.tv_alarmInfo;
import static com.jiangman.weather.R.id.tv_degree;
import static com.jiangman.weather.R.id.tv_location;
import static com.jiangman.weather.R.id.tv_updateTime;

/**
 * Created by secoro on 2017/4/26.
 */

public class WeatherFragment extends BaseFragment {

    @BindView(tv_location)
    TextView tvLocation;
    @BindView(iv_refresh)
    ImageView ivRefresh;
    @BindView(tv_RTTemp)
    TextView tvRTTemp;
    @BindView(tv_degree)
    TextView tvDegree;
    @BindView(tv_updateTime)
    TextView tvUpdateTime;
    @BindView(ll_updateTimeArea)
    LinearLayout llUpdateTimeArea;
    @BindView(iv_type)
    ImageView ivType;
    @BindView(tv_RTType)
    TextView tvRTType;
    @BindView(tv_alarmInfo)
    TextView tvAlarmInfo;
    @BindView(ll_alarmInfo)
    LinearLayout llAlarmInfo;
    @BindView(R.id.slide_tab)
    SlidingTabLayout slideTab;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(ll_manage)
    LinearLayout llManage;
    @BindView(lv_drawerMenu)
    ListView lvDrawerMenu;
    @BindView(R.id.swipe_left)
    SwipeRefreshLayout swipeLeft;
    @BindView(iv_settings)
    ImageView ivSettings;
    @BindView(R.id.drawer)
    DrawerLayout drawer;
    @BindView(R.id.home_bg)
    LinearLayout homeBg;
    @BindView(R.id.recycle_view_zhe)
    RecyclerView recycleViewZhe;
    @BindView(R.id.iv_drawer)
    ImageView ivDrawer;

    private LayoutInflater mInflater;
    private View viewRealTime;//实时天气
    private View viewForecast;//未来天气
    private View viewLivingIndex;//昨日天气
    private View viewYesterday;//生活指数
    private ArrayList<View> mViewList;//界面集合
    private ListView lv_forecast;
    private TextView tv_noForecastData;
    private ListView lv_livingIndex;
    private TextView tv_noLivingIndexData;
    private TextView tv_noYesterdayData;
    private ImageView iv_yesdayDayType;
    private TextView tv_yesdayDayType;
    private ImageView iv_yesdayNightType;
    private TextView tv_yesdayNightType;
    private LinearLayout ll_yesterday;
    private TextView tv_yesdayTempRange;
    private TextView tv_noRealTimeData;
    private VerticalDataDisplayView vddv_shidu;
    private TriangleDataDisplayView tddv_feng;
    private TriangleDataDisplayView tddv_sunriseset;
    private LinearLayout ll_realTime;
    private VerticalDataDisplayView vddv_aqi;
    private RelativeLayout rl_noAQIData;

    /**
     * 存取配置文件的SharedPreferences
     */
    private SharedPreferences sp;
    /**
     * 操作天气数据库的Dao
     */
    private WeatherDao dao;


    /**
     * 用于存放所有已选择的城市的实时天气的集合和areasNameList的位置一一对应
     */
    private ArrayList<String> mAreaRTTempList;
    /**
     * 存放左侧抽屉不同temp的icon集合
     */
    private ArrayList<Integer> mTempIconList;
    /**
     * 当前主area的WeatherId
     */
    private String mainWeatherId = "101010100";

    /**
     * 用于存放未来天气预报的集合
     */
    private ArrayList<WeatherForecast> mForecastList = new ArrayList<>();
    /**
     * 用于存放生活指数的集合
     */
    private ArrayList<WeatherZhiShu> mZhishuList = new ArrayList<>();
    /**
     * 用于存放生活指数对应的icon的集合
     */
    private ArrayList<Integer> resIdList = new ArrayList<>();
    /**
     * 用于存放所有已选择的城市的集合
     */
    private ArrayList<String> mAreasNameList = new ArrayList<>();
    /**
     * 自定义天气预报listview适配器
     */
    private MyForecastBaseAdapter mForecastAdapter;
    /**
     * 自定义生活指数listview适配器
     */
    private MyLivingIndexBaseAdapter mLivingIndexAdapter;
    private MyLocationBaseAdapter mLocationAdapter;
    private CompleteShowAdapter mCompleteShowAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_weather, null);
        mInflater = inflater;
        ButterKnife.bind(this, inflate);

        initData();
        initView();
        return inflate;
    }

    private void initData() {
        sp = getActivity().getSharedPreferences(getString(R.string.config), MODE_PRIVATE);
        dao = new WeatherDao(getActivity());

        mAreasNameList = dao.getAreasNameList();
        mAreaRTTempList = dao.getAreaRTTempList();
        mTempIconList = WeatherUtils.getTempIconIdList(mAreaRTTempList);
    }


    private void initView() {

        mViewList = new ArrayList<View>();
        viewRealTime = mInflater.inflate(R.layout.pager_real_time, null);
        viewForecast = mInflater.inflate(R.layout.pager_forecast, null);
        viewLivingIndex = mInflater.inflate(R.layout.pager_living_index, null);
        viewYesterday = mInflater.inflate(R.layout.pager_yesterday, null);

        mViewList.add(viewRealTime);
        mViewList.add(viewForecast);
        mViewList.add(viewYesterday);
        mViewList.add(viewLivingIndex);
        //// TODO: 2017/4/26  下面天气的　viewPager
        List<String> titles = new ArrayList<>();
        titles.add("实时天气");
        titles.add("未来天气");
        titles.add("昨日天气");
        titles.add("生活指数");
        viewPager.setAdapter(new MyPagerAdapter(mViewList, titles));
        slideTab.setViewPager(viewPager);
        initPagerViews();
        initPagerAdapter();
        initDrawer();
    }

    /**
     * 未来天气
     */
    private void initPagerAdapter() {
        mForecastAdapter = new MyForecastBaseAdapter(getActivity(), mForecastList);
        mLivingIndexAdapter = new MyLivingIndexBaseAdapter(getActivity(), mZhishuList, resIdList);
        int widthPx = DensityUtils.dp2px(getActivity(), 80);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
//        int widthPx=displayMetrics.widthPixels;
        mCompleteShowAdapter = new CompleteShowAdapter(mForecastList, 39, -8, widthPx);


    }


    /**
     * 实例化ViewPager中的控件
     */
    private void initPagerViews() {
        lv_forecast = (ListView) viewForecast.findViewById(R.id.lv_forecast);
        lv_forecast.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                enterForecastActivity(position);

            }
        });
//// TODO: 2017/5/4
        //天气周
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycleViewZhe.setLayoutManager(linearLayoutManager);
        recycleViewZhe.setAdapter(mCompleteShowAdapter);


        tv_noForecastData = (TextView) viewForecast.findViewById(R.id.tv_noForecastData);
        lv_livingIndex = (ListView) viewLivingIndex.findViewById(R.id.lv_livingIndex);
        // 实例化昨天天气控件
        tv_noLivingIndexData = (TextView) viewLivingIndex.findViewById(R.id.tv_noLivingIndexData);
        tv_noYesterdayData = (TextView) viewYesterday.findViewById(R.id.tv_noYesterdayData);
        iv_yesdayDayType = (ImageView) viewYesterday.findViewById(R.id.iv_yesdayDayType);
        tv_yesdayDayType = (TextView) viewYesterday.findViewById(R.id.tv_yesdayDayType);
        iv_yesdayNightType = (ImageView) viewYesterday.findViewById(R.id.iv_yesdayNightType);
        tv_yesdayNightType = (TextView) viewYesterday.findViewById(R.id.tv_yesdayNightType);
        ll_yesterday = (LinearLayout) viewYesterday.findViewById(R.id.ll_yesterday);
        tv_yesdayTempRange = (TextView) viewYesterday.findViewById(R.id.tv_yesdayTempRange);
        // 实例化实时天气天气控件
        tv_noRealTimeData = (TextView) viewRealTime.findViewById(R.id.tv_noRealTimeData);
        vddv_shidu = (VerticalDataDisplayView) viewRealTime.findViewById(R.id.vddv_shidu);
        tddv_feng = (TriangleDataDisplayView) viewRealTime.findViewById(R.id.tddv_feng);
        tddv_sunriseset = (TriangleDataDisplayView) viewRealTime.findViewById(R.id.tddv_sunriseset);
        ll_realTime = (LinearLayout) viewRealTime.findViewById(R.id.ll_realTime);
        vddv_aqi = (VerticalDataDisplayView) viewRealTime.findViewById(R.id.vddv_aqi);
        rl_noAQIData = (RelativeLayout) viewRealTime.findViewById(R.id.rl_noAQIData);
    }

    /**
     * 初始化抽屉
     */
    private void initDrawer() {

        //设置抽屉内的ListView
        mLocationAdapter = new MyLocationBaseAdapter(getActivity(), mAreasNameList, mAreaRTTempList, mTempIconList, dao);
        //// TODO: 2017/5/4
        lvDrawerMenu.setAdapter(mLocationAdapter);
        System.out.println("设置数据：" + mAreasNameList.size() + "---" + mAreaRTTempList.size() + "---" + mTempIconList.size());
        // listview点击事件
        lvDrawerMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取点击的area的id
                mainWeatherId = CityQueryDao.getWeatherIdByAreaName((String) mAreasNameList.get(position));
                // 获取点击的area的名称
                final String name = (String) mAreasNameList.get(position);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(500);
                        if (dao.haveCache(mainWeatherId)) {
                            // 点击的主area有本地缓存，显示数据
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    showData(mainWeatherId);
                                }
                            });
                            if (checkMainAreaNeedUpdate()) {
                                refreshMainAreaData();
                            }
                        } else {
                            // 否则请求网络数据
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    showNA();
                                    tvLocation.setText(name);
                                }
                            });
                            refreshMainAreaData();
                        }
                    }
                }).start();
                // 获取之前已经设置的主area id
                String oldMainAreaId = dao.getMainAreaId();
                // 获取之前已经设置的主area名称
                String oldMainAreaName = dao.getMainAreaName();
                // 将之前的主area的标记设为非主area
                dao.setMainArea(oldMainAreaId, 0);
                // 将新点击的area设置为主area
                dao.setMainArea(mainWeatherId, 1);
                // 通知适配器更新数据
                mLocationAdapter.notifyDataSetChanged();
            }
        });

        llManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进入city管理页面
                enterCityManage();
            }
        });


    }

    /**
     * 初始化Toolbar
     */
    private void initToolbar() {

    }

    /**
     * 检查主area是否需要更新
     *
     * @return 需要返回true，否则返回false
     */
    public boolean checkMainAreaNeedUpdate() {
        boolean need = false;
        if (System.currentTimeMillis() - Long.valueOf(dao.getLastUpdateTime(mainWeatherId)) < 3600000) {
        } else {
            need = true;
        }
        return need;
    }


    /**
     * 停止显示下拉刷新
     *
     * @param swipe 需要停止显示的下拉刷新控件
     */
    private void stopRefreshing(SwipeRefreshLayout swipe) {
        if (swipe.isRefreshing()) {
            swipe.setRefreshing(false);
        }
    }


    /**
     * 刷新主area天气数据
     */
    private void refreshMainAreaData() {

        new AsyncTask<String, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(String... params) {
                try {
                    String areaId = params[0];
                    String s = HttpUtils.requestData(getString(R.string.base_api_url) + areaId);
                    System.out.println("响应：" + s);
                    WeatherDataParser.parserToDB(getActivity(), s, areaId);
                    dao.setCache(mainWeatherId, 1);
                    dao.setLastUpdateTime(mainWeatherId, String.valueOf(System.currentTimeMillis()));
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }

            @Override
            protected void onPostExecute(Boolean isSuccess) {
                if (isSuccess) {
                    mainAreaRefreshSuccess();
                } else {
                    networkError();
                }
            }
        }.execute(mainWeatherId);
    }


    /**
     * 主城市刷新成功时调用
     */
    private void mainAreaRefreshSuccess() {
//        stopRefreshing(mHomeSwipe);
        Snackbar snackbar = Snackbar.make(tvLocation, "刷新成功", Snackbar.LENGTH_SHORT);
        SnackBarUtils.customSnackBar(snackbar, getResources().getColor(R.color.home_snack_bar_background), getResources().getColor(R.color.home_snack_bar_text), 0);
        snackbar.show();
        showData(mainWeatherId);
    }


    /**
     * 网络错误时调用
     */
    private void networkError() {
//        stopRefreshing(mHomeSwipe);
//        stopRefreshing(mLeftSwipe);
        Snackbar snackbar = Snackbar.make(tvLocation, "请检查网络设置", Snackbar.LENGTH_SHORT);
        SnackBarUtils.customSnackBar(snackbar, getResources().getColor(R.color.home_snack_bar_background), getResources().getColor(R.color.home_snack_bar_text), 0);
        snackbar.show();
    }


    /**
     * 显示数据在界面上  、
     *
     * @param weatherId 要显示的areaid
     */
    //// TODO: 2017/5/4
    private void showData(String weatherId) {

        if (dao.haveCache(weatherId)) {
            // ----------显示实时天气部分(ViewPager上方部分)-----------
            WeatherRealtime realtime = dao.getDataFromRealtime(weatherId);
            tvDegree.setVisibility(View.VISIBLE);
            String updateTime = realtime.getUpdatetime();
            String wendu = realtime.getWendu();
            tvRTTemp.setText(wendu);
            tvUpdateTime.setText(updateTime + " 发布");
            llUpdateTimeArea.setVisibility(View.VISIBLE);
            mForecastList.clear();
            mForecastList.addAll(dao.getDataListFromForecast(weatherId));
            mForecastAdapter.notifyDataSetChanged();
            //// TODO: 2017/5/4   dedeng
            mCompleteShowAdapter.notifyDataSetChanged();
            String realTimeTypeName = realtime.getWeather();
            tvRTType.setText(realTimeTypeName);
            int resId = WeatherUtils.getWhiteIconIdByTypeName(realTimeTypeName);
            if (resId != 0) {
                ivType.setImageResource(resId);
                ivType.setVisibility(View.VISIBLE);
            } else {
                ivType.setVisibility(View.GONE);
            }
            // ----------根据是否有预警信息显示预警信息--------
            if (dao.haveAlarm(weatherId)) {
                llAlarmInfo.setVisibility(View.VISIBLE);
                tvAlarmInfo.setText(dao.getSimpleAlarmDesc(weatherId));
            } else {
                llAlarmInfo.setVisibility(View.GONE);
            }
            // -----------显示天气预报部分-------------
            if (lv_forecast.getAdapter() == null) {
                lv_forecast.setAdapter(mForecastAdapter);
            } else {
                mForecastAdapter.notifyDataSetChanged();
            }
            if (recycleViewZhe.getAdapter() == null) {
                recycleViewZhe.setAdapter(mCompleteShowAdapter);
            } else {
                mCompleteShowAdapter.notifyDataSetChanged();
            }
            tv_noForecastData.setVisibility(View.INVISIBLE);
            lv_forecast.setVisibility(View.VISIBLE);
            // -----------显示生活指数部分--------------
            mZhishuList.clear();
            mZhishuList.addAll(dao.getDataListFromZhishu(weatherId));
            resIdList.clear();
            resIdList.addAll(WeatherUtils.getWhiteLivingIndexIconList(mZhishuList));
            if (lv_livingIndex.getAdapter() == null) {
                lv_livingIndex.setAdapter(mLivingIndexAdapter);
            } else {
                mLivingIndexAdapter.notifyDataSetChanged();
            }
            tv_noLivingIndexData.setVisibility(View.INVISIBLE);
            lv_livingIndex.setVisibility(View.VISIBLE);
            // -----------显示昨日天气部分--------------
            ll_yesterday.setVisibility(View.VISIBLE);
            tv_noYesterdayData.setVisibility(View.INVISIBLE);
            WeatherYesterday yesterday = dao.getDataFromYesterday(weatherId);
            String yesdayLow = yesterday.getTempMin();
            String yesdayHigh = yesterday.getTempMax();
            tv_yesdayTempRange.setText(yesdayLow + " ° ~ " + yesdayHigh + " °C");
            String yesdayDayType = yesterday.getWeatherStart();
            iv_yesdayDayType.setImageResource(WeatherUtils.getWhiteIconIdByTypeName(yesdayDayType));
            tv_yesdayDayType.setText(yesdayDayType);
            String yesdayNightType = yesterday.getWeatherEnd();
            iv_yesdayNightType.setImageResource(WeatherUtils.getWhiteIconIdByTypeName(yesdayNightType));
            tv_yesdayNightType.setText(yesdayNightType);
            // ----------------显示实时天气ViewPager部分-----------------
            ll_realTime.setVisibility(View.VISIBLE);
            tv_noRealTimeData.setVisibility(View.INVISIBLE);
            vddv_shidu.setValueText(realtime.getShidu());
            tddv_feng.setLeftValue(realtime.getFengxiang());
            tddv_feng.setRightValue(realtime.getFengli());
            tddv_sunriseset.setLeftValue(realtime.getSunrise());
            tddv_sunriseset.setRightValue(realtime.getSunset());
            WeatherAQI aqi = dao.getDataFromAQI(weatherId);
            if (dao.haveAQI(weatherId)) {
                vddv_aqi.setVisibility(View.VISIBLE);
                rl_noAQIData.setVisibility(View.INVISIBLE);
                vddv_aqi.setTitleText(aqi.getQuality());
                vddv_aqi.setValueText(aqi.getAqi());
                vddv_aqi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        enterAQIActivity();
                    }
                });
            } else {
                vddv_aqi.setVisibility(View.INVISIBLE);
                rl_noAQIData.setVisibility(View.VISIBLE);
            }

        } else {
            showNA();
        }
        // whatever,都要显示主area的位置在主界面上
        tvLocation.setText(CityQueryDao.getAreaNameByWeatherId(mainWeatherId));
        updateList();
        mLocationAdapter.notifyDataSetChanged();
    }


    /**
     * 显示N/A界面
     */
    public void showNA() {
        // 判断当前是否已经至少选择一个城市
        if (!sp.getBoolean("area_selected", false)) {
            tvLocation.setText("N/A");
        }
        tvDegree.setVisibility(View.INVISIBLE);
        tvRTTemp.setText("");
        llUpdateTimeArea.setVisibility(View.INVISIBLE);
        tvRTType.setText("");
        ivType.setVisibility(View.GONE);
        updateList();
        mLocationAdapter.notifyDataSetChanged();
        lv_forecast.setVisibility(View.INVISIBLE);
        tv_noForecastData.setVisibility(View.VISIBLE);
        lv_livingIndex.setVisibility(View.INVISIBLE);
        tv_noLivingIndexData.setVisibility(View.VISIBLE);
        llAlarmInfo.setVisibility(View.GONE);
        tv_noYesterdayData.setVisibility(View.VISIBLE);
        ll_yesterday.setVisibility(View.INVISIBLE);
        ll_realTime.setVisibility(View.INVISIBLE);
        tv_noRealTimeData.setVisibility(View.VISIBLE);
    }

    /**
     * 更新area集合、area rt temp集合、area temp icon集合
     */
    private void updateList() {
        mAreasNameList = dao.getAreasNameList();
        mAreaRTTempList = dao.getAreaRTTempList();
        mTempIconList = WeatherUtils.getTempIconIdList(mAreaRTTempList);
    }


    @OnClick(iv_refresh)
    public void onClick() {
        if (TextUtils.isEmpty(mainWeatherId)) {
            Toast.makeText(getActivity(), "请选择一个城市", Toast.LENGTH_SHORT).show();
//
//            Snackbar snackbar = Snackbar.make(tv_location, "请添加一个城市", Snackbar.LENGTH_SHORT);
//            SnackBarUtils.customSnackBar(snackbar, getResources().getColor(R.color.home_snack_bar_background), getResources().getColor(R.color.home_snack_bar_text), 0);
//            snackbar.show();
        } else {
            RotateAnimation ra = new RotateAnimation(0, 720, ivRefresh.getWidth() / 2, ivRefresh.getHeight() / 2);
            ra.setDuration(2000);
            ivRefresh.startAnimation(ra);
            refreshMainAreaData();
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        // 初始化相关设置
        // ---------------显示Notification部分--------------
        // ---------------显示桌面插件Widget部分--------------
        // 判断当前是否已经至少选择一个城市
        if (!sp.getBoolean("area_selected", false)) {
            Snackbar snackbar = Snackbar.make(tvLocation, "空空如也~", Snackbar.LENGTH_INDEFINITE);
            SnackBarUtils.customSnackBar(snackbar, getResources().getColor(R.color.home_snack_bar_background), getResources().getColor(R.color.home_snack_bar_text), Color.YELLOW);
            snackbar.setAction("立即添加城市", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    enterSelectCity();
                }
            });
            snackbar.show();
            showNA();
        }
        // 通知适配器更新数据
        mLocationAdapter.notifyDataSetChanged();
        // 获取主areaId
        mainWeatherId = dao.getMainAreaId();
        if (!TextUtils.isEmpty(mainWeatherId)) {
            if (dao.haveCache(mainWeatherId)) {
                showData(mainWeatherId);
                if (checkMainAreaNeedUpdate()) {
                    refreshMainAreaData();
                }
            } else {
                tvLocation.setText(dao.getMainAreaName());
                showNA();
                refreshMainAreaData();
            }
        }
    }


    /**
     * 进入选择城市页面
     */
    private void enterSelectCity() {
        Intent intent = new Intent(getActivity(), ChooseCityActivity.class);
//        AnimationUtils.startActivityWithExplodeAnimation(this, intent);
        startActivity(intent);
    }

    /**
     * 进入城市管理界面
     */
    private void enterCityManage() {
        Intent intent = new Intent(getActivity(), CityManageActivity.class);
//        AnimationUtils.startActivityWithExplodeAnimation(this, intent);
        startActivity(intent);
    }


    boolean isOpen=false;
    @OnClick(R.id.iv_drawer)
    public void onClick2() {
        if(isOpen){
            drawer.closeDrawers();
            isOpen=false;
        }else {
            drawer.openDrawer(Gravity.LEFT);
            isOpen=true;
        }


    }
}
